<template>
	<view>
		<view :style="{height: screenTop+'px'}" style="background: #f1f1f1;"></view>
		<uni-nav-bar backgroundColor="#f1f1f1" :title="navTitle">
			<view slot="left">
				<uni-icons @click="backAction()" type="back" size="25"></uni-icons>
			</view>
		</uni-nav-bar>
		<view class="u-f-ajc u-f-c noData">
			<image :src="userInfo.gif" mode=""></image>
			<text>{{userInfo.desc}}</text>
		</view>
	</view>
</template>

<script>
	const {
		turnTenToSixteen
	} = require('@/utils/util.js');
	const { handleStartErr,handledoorLockTransparent } = require('@/jvss/pagesDevice/utils/index.js');
	export default {
		data() {
			return {
				device: {},
				navTitle: '',
				userInfo: {},
				screenTop: 0,
				isAdmin: false, //是否为管理员
				isAdded: false, //设备端是否添加
				unload: false, //是否返回上一页
				isReturn:false, //是否取消
			}
		},
		onLoad(opt) {
			const sys = uni.getSystemInfoSync();
			this.screenTop = sys.statusBarHeight
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			this.userInfo = JSON.parse(opt.userInfo)
			this.isAdmin = opt.admin == 'admin' ? true : false
			this.navTitle = '添加' + this.userInfo.title
			this.addUser('AddUser')
		},
		onUnload() {
			if (!this.unload) {
				console.log('addonUnload')
				this.isReturn = true
				this.setCancelAdd();
			}
		},
		methods: {
			addUser(action, callback) {
				const data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "DoorUser",
						DoorUser: {
							Action: action,
							Auth: this.isAdmin ? 0 : 1,
							Type: this.userInfo.type,
						}
					}
				};
				const token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					this.unload = true;
					this.isAdded = true;
					if(this.isReturn) return;
					console.log(result, '添加用户')
					handledoorLockTransparent(result,(res,Err)=>{
						console.log(res,Err,'handledoorLockTransparent')
						if(Err == '0x00'){
							uni.showToast({
								title: '用户添加成功',
								icon: 'success',
								duration: 2000
							})
						}else{
							uni.showToast({
								title: getApp().globalData.errCodeMsg.wakeUpLockBoardCodeMsg[Err] || result.data.msg,
								icon: 'none',
								duration: 2000
							});
						}
						setTimeout(() => {
							uni.navigateBack({})
						}, 1000)    
					},true,(error)=>{
						setTimeout(() => {
							uni.navigateBack({})
						}, 1000)
					})
				}, false );
			},
			setCancelAdd() {
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "DoorUser",
						DoorUser: {
							Action: 'CancelAdd',
							Auth: this.isAdmin ? 0 : 1,
							Type: this.userInfo.type,
						}
					}
				};
				let token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					result = result.data;
					console.log(result, 'CancelAdd')
				}, false);
			},
			//返回判断
			backAction() {
				if (this.isAdded) {
					return uni.navigateBack({});
				}
				uni.showModal({
					title: '提示',
					content: '是否中止添加用户？',
					success: (res) => {
						if (res.confirm) {
							this.unload = true;
							this.isReturn = true
							this.setCancelAdd();
							uni.navigateBack({});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.noData {
		transform: translateY(50%);

		image {
			width: 400rpx;
			height: 400rpx;
		}

		text {
			margin-top: 40rpx;
			color: #000;
			font-size: 28rpx;
			text-align: center;
			width: 450rpx;
		}
	}
</style>